<?php
class Login extends Base{
	public function index(){
		
		if (isset($_POST['username'])){
			$name=$_POST['username'];
			$password=trim($_POST['password']);
			
			if('admin' != $password){
					$data['info'] = '密码错误';
					$data ['status'] = 0;
					$data ['url'] = '';
					echo json_encode ($data);
					exit;
				 
			}
			
			if ($name !='admin'){
				$data['info'] = '用户名不存在';
				$data ['status'] = 0;
				$data ['url'] = '';
				echo json_encode ($data);
				exit;
			}
			header ( 'Content-Type:application/json; charset=utf-8' );
			$data ['info'] = '登入成功';
			$data ['status'] = 1;
			$data ['url'] = '?a=home';
			echo json_encode ( $data);
			$_SESSION['username']=$name;
			exit;
		}
		
		include_once(_template());
	}
	public function home(){
		$this->db();
		$name=NULl;
		if (isset($_GET['name']) && !empty($_GET['name'])){
			$name=$_GET['name'];
		}
		 
		
		include_once(_template());
	}
	
	public function add(){
		 
		if (isset($_POST['title']) && !empty($_POST['title'])){
			$this->db();
			  
			$title=addslashes(trim($_POST['title']));
			$nianji=addslashes(trim($_POST['nianji']));
			$step=addslashes(trim($_POST['step']));
			$da=addslashes(trim($_POST['da']));
			$db=addslashes(trim($_POST['db']));
			$dc=addslashes(trim($_POST['dc']));
			$dd=addslashes(trim($_POST['dd']));
			$de=addslashes(trim($_POST['de']));
			$df=addslashes(trim($_POST['df']));
			$dan=strtolower(addslashes(trim($_POST['dan'])));
			
			$sql="select count(id) as count from `ti` where nianji='".$nianji."' and step='".$step."' ";
			$query_count= $this->mysqli->query($sql);
			$countInfo=$query_count->fetch_array();
			$tiId=$countInfo['count']+1;

			$this->mysqli->query("insert into ti(tid,title,nianji,step,da,db,dc,dd,de,df,dan)
				values(".$tiId.",'".$title."', '".$nianji."', '".$step."', '".$da."', '".$db."', '".$dc."', 
					'".$dd."', '".$de."','".$df."','".$dan."') ");
			 
			echo json_encode(array('s'=>'yes'));
			exit;
		}
		 
		include_once(_template());
	}
	
	public function edit(){
		if (isset($_GET['id']) && !empty($_GET['id'])){
			 $id=$_GET['id'];
		}else{
			exit;
		}
		$this->db();
		$sql="select * from ti where id='".$id."' limit 0,1 ";
		$query = $this->mysqli->query($sql);
		$tiInfo=$query->fetch_array();
 
		include_once(_template());
	}
	
	public function view(){
		if (isset($_GET['id']) && !empty($_GET['id'])){
			$id=$_GET['id'];
		}else{
			exit;
		}
		$this->db();
		$sql="select * from piao where id='".$id."' limit 0,1 ";
		$query = $this->mysqli->query($sql);
		$userInfo=$query->fetch_array();
	
		$querywx= $this->mysqli->query("select nickname,headimgurl from users where id='".$userInfo['uid']."' limit 0,1 ");
		$wxInfo=$querywx->fetch_array();
	
	
		include_once(_template());
	}
	
	public function pay(){
		$this->db();
		$name=NULl;
		if (isset($_GET['name']) && !empty($_GET['name'])){
			$name=$_GET['name'];
		}
			
		include_once(_template());
	}
	
	public function dopay(){
		if (isset($_GET['id']) && !empty($_GET['id'])){
			$id=$_GET['id'];
		}else{
			exit;
		}
		$this->db();
		$sql="update piao set ispay='agree' where id='".$id."' ";
		$query = $this->mysqli->query($sql);
		header('Location: ?a=pay');
		exit;
	}
	
	public function save(){
		if (isset($_GET['id']) && !empty($_GET['id'])){
			$id=$_GET['id'];
		}else{
			exit;
		}

		$title=addslashes(trim($_POST['title']));
		$nianji=addslashes(trim($_POST['nianji']));
		$step=addslashes(trim($_POST['step']));
		$da=addslashes(trim($_POST['da']));
		$db=addslashes(trim($_POST['db']));
		$dc=addslashes(trim($_POST['dc']));
		$dd=addslashes(trim($_POST['dd']));
		$de=addslashes(trim($_POST['de']));
		$df=addslashes(trim($_POST['df']));
		$dan=strtolower(addslashes(trim($_POST['dan'])));
		 
		$this->db();
		$sql="update ti set title='".$title."',nianji='".$nianji."'
				, step='".$step."',da='".$da."', db='".$db."', dc='".$dc."',
			dd='".$dd."',de='".$de."',df='".$df."',dan='".$dan."' 	where id='".$id."'   ";
		 
		$query = $this->mysqli->query($sql);
	 
		echo json_encode(array('stauts'=>'ok'));
	}
	
	public function logout(){
		 unset($_SESSION['username']);
		 session_destroy();
		 header('Location: ?login');
		 exit;
	}


}
?>